#include "mainwidget.h"
#include "ui_mainwidget.h"



MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowTitle("OneChat");
    nowmessagecount=0;
    nowcontactcount=0;
    ui->label_newcount->hide();
    ui->label_newcount_contact->hide();
    PageManger::switchToPage(ui->widget,PageManger::CHATPAGE);
    ui->btn_avatar->setIcon(AvatarLib::getInstance()->getAvatar("user"));
    nowchecked=ui->btn_chat;
    connect(AvatarLib::getInstance(),&AvatarLib::s_avatarFresh,[=](){
        ui->btn_avatar->setIcon(AvatarLib::getInstance()->getAvatar("user"));
    });
    connect(PageManger::getChatWidget(),&ChatWidget::s_addCount,[=](){
        ui->label_newcount->show();
        nowmessagecount++;
        ui->label_newcount->setNum(nowmessagecount);
    });
    connect(PageManger::getChatWidget(),&ChatWidget::s_subCount,this,&MainWidget::subChatCount);
    connect(FriendApplyLib::getInstance(),&FriendApplyLib::s_haveNewApply,this,[=](const QString& friendoc,const QString& friendname){
        Q_UNUSED(friendoc);
        Q_UNUSED(friendname);
        if(!ui->btn_contact->isChecked())
        addContactCount();
    });
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::setCurrentBtnOfPage(int index)
{
    switch(index){
    case 1:
        nowchecked->setChecked(false);
        nowchecked=ui->btn_chat;
        ui->btn_chat->setChecked(true);
        break;
    case 2:
        break;
    case 3:
        break;
    case 4:
        break;
    default:
        break;
    }
}
//以下为拖拽窗口代码
void MainWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(event->y()<50){
        m_bDrag = true;
        //获得鼠标的初始位置
        mouseStartPoint = event->globalPos();
        //获得窗口的初始位置
        windowTopLeftPoint = this->frameGeometry().topLeft();
        }
    }
}

void MainWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDrag)
    {
        //获得鼠标移动的距离
        QPoint distance = event->globalPos() - mouseStartPoint;
        //改变窗口的位置
        this->move(windowTopLeftPoint + distance);
    }
}

void MainWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = false;
    }
}

void MainWidget::addContactCount()
{
    nowcontactcount++;
    ui->label_newcount_contact->show();
    ui->label_newcount_contact->setNum(nowcontactcount);
}

void MainWidget::on_btn_min_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}

void MainWidget::on_btn_close_clicked()
{
    ThreadManger::getInstance()->writeChatBufNow();
    ThreadManger::getInstance()->refreshLastestDB(ChatWidget::getChangeSet());
    ThreadManger::getInstance()->destroyThreadManger();
    ThreadManger::getInstance()->updateUserOcFile();
    ThreadManger::getInstance()->flushSettings();
    this->close();
}

void MainWidget::on_btn_contact_clicked()
{
    if(nowchecked==ui->btn_contact)
    {
        ui->btn_contact->setChecked(true);
        return;
    }

    PageManger::switchToPage(ui->widget,PageManger::CONTACTPAGE);
    nowchecked->setChecked(false);
    nowchecked=ui->btn_contact;

    ui->label_newcount_contact->hide();
    nowcontactcount=0;
}

void MainWidget::on_btn_chat_clicked()
{
    if(nowchecked==ui->btn_chat)
    {
        ui->btn_chat->setChecked(true);
        return;
    }

    PageManger::switchToPage(ui->widget,PageManger::CHATPAGE);
    //获得当前会话的数量，总的减去
    subChatCount(PageManger::getChatWidget()->getTmpCount());
    nowchecked->setChecked(false);
    nowchecked=ui->btn_chat;

}

void MainWidget::on_btn_favorites_clicked()
{
    if(nowchecked==ui->btn_favorites)
    {
        ui->btn_favorites->setChecked(true);
        return;
    }

    PageManger::switchToPage(ui->widget,PageManger::FAVORITESPAGE);
    nowchecked->setChecked(false);
    nowchecked=ui->btn_favorites;
}

void MainWidget::on_btn_setting_clicked()
{
    if(nowchecked==ui->btn_setting)
    {
        ui->btn_setting->setChecked(true);
        return;
    }
    PageManger::switchToPage(ui->widget,PageManger::SETTINGPAGE);
    nowchecked->setChecked(false);
    nowchecked=ui->btn_setting;
}

void MainWidget::subChatCount(int count)
{
    nowmessagecount-=count;
    if(nowmessagecount!=0)
        ui->label_newcount->setNum(nowmessagecount);
    else
        ui->label_newcount->hide();
}

void MainWidget::on_btn_avatar_clicked()
{

}


